package com.feicheng.lanniao.service.impl;


import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarBuyClientVo;
import com.feicheng.lanniao.entity.CarBuyInfo;
import com.feicheng.lanniao.entity.CarBuyOwner;
import com.feicheng.lanniao.entity.CarOwner;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.CarBuyClientVoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarBuyClientVoService;
import com.feicheng.lanniao.service.ICarBuyInfoService;
import com.feicheng.lanniao.service.ICarOwnerService;
import com.feicheng.lanniao.service.ICarBuyOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class CarBuyClientVoServiceImpl extends ServiceImpl<CarBuyClientVoMapper, CarBuyClientVo> implements ICarBuyClientVoService {
	@Autowired
	ICarBuyInfoService iCarBuyInfoService;
	/**
	 * @see.ICarBuyClientVoService.getCarBuyClientVoList
	 */
	@Override
	public ResultInfo getCarBuyClientVoList(String title,String details,Integer clientId , Integer currentPage, Integer pageSize) {
		QueryWrapper<CarBuyClientVo> carQueryWapper = new QueryWrapper<CarBuyClientVo>();
		if(StringUtils.hasLength(title)) {
			carQueryWapper.like("title", title);
		}
		if (StringUtils.hasLength(details)) {
			carQueryWapper.like("details", details);
		}
		//判断查询条件clientId是否为空或0，不为空或0则与数据名字关联
		if(null !=clientId && clientId!=0) {
			carQueryWapper.eq("client_id", clientId);	
		}		
		IPage<CarBuyClientVo> pageInfo = new Page<CarBuyClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, carQueryWapper));
	}
	/**
	 * @see.ICarBuyClientVoService.deleteCarBuyInfo
	 */
	@Override
	public ResultInfo deleteCarBuyInfo(Integer cbId) {
		//如果cbId为空或0，抛出异常
		if(null==cbId || 0 ==cbId) {
			throw new SystemException("cbId为空");
		}
		// 构建查询条件
		QueryWrapper<CarBuyInfo> carQueryWapper = new QueryWrapper<CarBuyInfo>();
		// 把主键加入查询条件
		carQueryWapper.eq("cb_id", cbId);
		 // 根据主键查询出一条数据，用一个对象接收
		CarBuyInfo carBuyInfo = iCarBuyInfoService.getOne(carQueryWapper);
		// 设置删除位值
		carBuyInfo.setDelFlag(false);
		// 修改买车信息
		Boolean b = iCarBuyInfoService.updateById(carBuyInfo);
		// 返回修改结果
		if (!b) {
			throw new SystemException("删除失败");
		}
		return ResultInfo.success();
	}
	
	/**
	 * @see.ICarBuyClientVoService.getOneCarBuyInfo
	 */
	@Override
	public ResultInfo getOneCarBuyInfo(Integer cbId) {
		//判断cbId不为空 则根据cbId查询1条买车信息
		if(cbId != null) {
			QueryWrapper<CarBuyClientVo> queryWrapper = new QueryWrapper<CarBuyClientVo>();
			// TODO Auto-generated method stub
			queryWrapper.eq("cb_id", cbId);
			return ResultInfo.success(this.getOne(queryWrapper));
		}
		else {
			return null;
		}
		
	}
	/**
	 * @see.ICarBuyClientVoService.editOneCarBuyInfo
	 */
	@Override
	public ResultInfo editOneCarBuyInfo(CarBuyInfo carBuyInfo) {
		if(null == carBuyInfo.getClientId()  || carBuyInfo.getClientId() == 0) {
			throw new SystemException("clientId缺失");
		}
		if (null == carBuyInfo.getTel() || carBuyInfo.getTel().length() != 11) {
			throw new SystemException("请输入11位手机号");
		}
		if (null == carBuyInfo.getWxNumber()) {
			throw new SystemException("请输入微信号");
		}
		if (null == carBuyInfo.getTitle()) {
			throw new SystemException("请输入标题");
		}
		return ResultInfo.success(iCarBuyInfoService.updateById(carBuyInfo));
	}
	@Autowired
	private ICarBuyOwnerService iCarBuyOwnerService;
	/**
	 * @see.ICarBuyClientVoService.insertOneCarInfo
	 */
	@Override
	public ResultInfo insertOneCarInfo(String tel,String wxNumber,String title,String details,Integer clientId) {
		//判断clientId是否为空或0 如果为空或者0 报异常
		if(null == clientId  || clientId == 0) {
			throw new SystemException("clientId缺失");
		}
		if (null == tel || tel.length() != 11) {
			throw new SystemException("请输入11位手机号");
		}
		if (null == wxNumber) {
			throw new SystemException("请输入微信号");
		}
		if (null == title) {
			throw new SystemException("请输入标题");
		}
		CarBuyInfo carBuyInfo = new CarBuyInfo();
		carBuyInfo.setTitle(title);
		carBuyInfo.setDetails(details);
		carBuyInfo.setTel(tel);
		carBuyInfo.setWxNumber(wxNumber);
		iCarBuyInfoService.save(carBuyInfo);
		
		CarBuyOwner carBuyOwner = new CarBuyOwner();
		// TODO Auto-generated method stub
		carBuyOwner.setClientId(clientId);
		carBuyOwner.setCbId(carBuyInfo.getCbId());
		System.out.println(carBuyOwner.getCbId() + carBuyOwner.getClientId());
		iCarBuyOwnerService.save(carBuyOwner);
		return ResultInfo.success();
	}
	
}
